package com.nars.datastructures;

public class Tree<E extends Comparable<E>> {

	TreeElement<E> rootElement;
	
	//Search
	public TreeElement<E> search(E value) {
		
		TreeElement<E> tempEmlement = rootElement;
		boolean found = false;
		Comparable<E> v = (Comparable<E>)value;
		do {
			if (tempEmlement.value == value) {
				found = true;
			} else if (v.compareTo(tempEmlement.value) > 0) {
				tempEmlement = tempEmlement.right;
			} else if (v.compareTo(tempEmlement.value) < 0) {
				tempEmlement = tempEmlement.left;
			}
		} while(found);
		
		return tempEmlement;
	};
	
	//Search Recursively
	public void search(TreeElement<E> node, Comparable<E> value) {
		if (value == node.value) {
			System.out.println(node.value);
		} else if (value.compareTo(node.value) > 0) {
			search(node.right, value);
		} else if (value.compareTo(node.value) < 0) {
			search(node.left, value);
		}		
	}	
	
	//Print All nodes in the tree/subtrees
	public void visitInOrder(TreeElement<E> node) {
		
		System.out.println(node.value);
	
		if (node.left != null) {
			visitInOrder(node.left);
		}
		if (node.right != null) {
			visitInOrder(node.right);
		}
	} 
	
	// insert;
	
	
	
	// delete;
	// search;
}
